<?php

namespace app\models;

use think\Model;

/**
 * Class ScheduleModel.
 */
class RefundAmountModel extends Model
{
    protected $table = 'edu_refund_amount';


    /**
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getrefundlist($param,$status)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        $where['status'] = 0;
        if (isset($param['student_name']) && '' !== $param['student_name']) {
            $where['student_name'] = ['like', '%'.$param['student_name'].'%'];
        }

        //校区
        if (isset($param['campus_id']) && '' !== $param['campus_id']) {
            $where['campus_id'] = $param['campus_id'];
        }else{
            if(get_current_type() == 4 || get_current_type() == 5){/*校区员工数据权限*/
                $where['campus_id'] = get_current_campus_id();
            }elseif (get_current_type() == 3 || get_current_type() == 2){/*系统区域数据权限*/
                $where['campus_id'] = ['in',get_current_campus_id()];
            }
        }

        //校区
        if (isset($param['campus_id']) && '' !== $param['campus_id']) {
            $where['campus_id'] = ['like', '%'.$param['campus_id'].'%'];
        }

        //退款通道
        if (isset($param['refund_channel']) && '' !== $param['refund_channel']) {
            $where['refund_channel'] = ['like', '%'.$param['refund_channel'].'%'];
        }


        //订单编号
        if (isset($param['contract_number']) && '' !== $param['contract_number']) {
            $where['contract_number'] = ['like', '%'.$param['contract_number'].'%'];
        }

        //退款凭证

        if (isset($param['receipt_number']) && '' !== $param['receipt_number']) {
            $where['receipt_number'] = ['like', '%'.$param['receipt_number'].'%'];
        }

        //时间段
        if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
            $where['update_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
        }
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}
        $listData = self::where($where)->order('create_time desc')
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        $amount = 0;
        if (!empty($listData)) {

            foreach ($listData['data'] as &$item) {
                $item['refund_channel_text'] = DictionaryModel::get(['id' => $item['refund_channel'] ])['name'];
                $item['balance'] = $item['balance']/100;
                $item['amount'] =  $item['amount'] /100;
                $signing_type = ContractModel::get(['contract_number' => $item['contract_number']])['signing_type'];
                $item['signing_type_text'] = DictionaryModel::where('id',$signing_type)->value('name');
                $item['experienced_person'] = UserModel::get(['id' => $item['experienced_person'] ])['name'];
                $item['total_amount'] = $item['total_amount']/100;
                $item['refund_amount'] = ($item['refund_amount']/100);
                if($signing_type == 114){
                    $item['total_amount'] = '-'; //储值
                    $item['amount'] = '-';
                }
                $amount = $amount + $item['refund_amount'];
            }
        }
        if($status){
            $item_end['receipt_number'] = '合计';
            $item_end['refund_channel_text'] = '-';
            $item_end['balance'] = '-';
            $item_end['amount'] =  '-';
            $item_end['total_amount'] = '-';
            $item_end['contract_number'] = '-';
            $item_end['campus_name'] = '-';
            $item_end['student_name'] = '-';
            $item_end['experienced_person'] = '-';
            $item_end['update_time'] = '-';
            $item_end['refund_amount'] = $amount;
            $listData['data'][count($listData['data'])] = $item_end;
        }

        return $listData;
    }

}
